package com.easy.customer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Customer;
import com.easy.common.bean.CustomerContact;
import com.easy.common.bean.CustomerInteraction;
import com.easy.common.enumbean.Gender;
import com.easy.common.service.ICustomerService;
import com.easy.customer.mapper.CustomerContactMapper;
import com.easy.customer.mapper.CustomerInteractionMapper;
import com.easy.customer.mapper.CustomerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerService implements ICustomerService {

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    private CustomerContactMapper customerContactMapper;

    @Autowired
    private CustomerInteractionMapper customerInteractionMapper;

    private static final Logger logger = LoggerFactory.getLogger(CustomerService.class);

    // 可以连着联系方式一起插入进去
    @Override
    public Customer insert(Customer customer) {
        logger.info("插入的 gender 值: {}", customer.getGender());
        customerMapper.insert(customer);
        Integer customerId = customer.getId();
        // 检查 contacts 是否为空
        if (customer.getContacts() != null) {
            for (CustomerContact contact : customer.getContacts()) {
                contact.setCustomerId(customerId);
                customerContactMapper.insert(contact);
            }
        }
        Customer result = this.getById(customerId);
        return result;
    }

    @Override
    public Customer update(Customer customer) {
        customerMapper.updateById(customer);
        Integer customerId = customer.getId();
        // 先删除原有的联系方式
        QueryWrapper<CustomerContact> contactQueryWrapper = new QueryWrapper<>();
        contactQueryWrapper.eq("customer_id", customerId);
        customerContactMapper.delete(contactQueryWrapper);

        // 插入新的联系方式
        if (customer.getContacts() != null) {
            for (CustomerContact contact : customer.getContacts()) {
                contact.setCustomerId(customerId);
                customerContactMapper.insert(contact);
            }
        }
        Customer result = this.getById(customerId);
        return result;
    }

    @Override
    public int delete(Integer id) {
        // 先删除关联的 CustomerContact 记录
        QueryWrapper<CustomerContact> contactQueryWrapper = new QueryWrapper<>();
        contactQueryWrapper.eq("customer_id", id);
        customerContactMapper.delete(contactQueryWrapper);

        // 再删除关联的 CustomerInteraction 记录
        QueryWrapper<CustomerInteraction> interactionQueryWrapper = new QueryWrapper<>();
        interactionQueryWrapper.eq("customer_id", id);
        customerInteractionMapper.delete(interactionQueryWrapper);

        // 最后删除 Customer 记录
        return customerMapper.deleteById(id);
    }

    @Override
    public Customer getById(Integer id) {
        Customer customer = customerMapper.selectById(id);
        if (customer != null) {
            QueryWrapper<CustomerContact> contactQueryWrapper = new QueryWrapper<>();
            contactQueryWrapper.eq("customer_id", id);
            List<CustomerContact> contacts = customerContactMapper.selectList(contactQueryWrapper);
            customer.setContacts(contacts);
        }
        return customer;
    }

    @Override
    public IPage<Customer> getListByPage(Page page, Customer customer) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>(customer);
        IPage<Customer> result = customerMapper.selectPage(page, queryWrapper);
/*        List<Customer> customerList = result.getRecords();
        for (Customer c : customerList) {
            QueryWrapper<CustomerContact> contactQueryWrapper = new QueryWrapper<>();
            contactQueryWrapper.eq("customer_id", c.getId());
            List<CustomerContact> contacts = customerContactMapper.selectList(contactQueryWrapper);
            c.setContacts(contacts);
        }*/
        return result;
    }

    @Override
    public List<Customer> getList(Customer customer) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>(customer);
        List<Customer> list = customerMapper.selectList(queryWrapper);
/*        for (Customer c : list) {
            QueryWrapper<CustomerContact> contactQueryWrapper = new QueryWrapper<>();
            contactQueryWrapper.eq("customer_id", c.getId());
            List<CustomerContact> contacts = customerContactMapper.selectList(contactQueryWrapper);
            c.setContacts(contacts);
        }*/
        return list;
    }
}